package cn.lyw.usermessage.service.service.impl;

import cn.common.beancopy.util.BeanCopierUtil;
import cn.common.encryption.EncryptUtil;
import cn.common.redis.RedisServie;
import cn.common.restresult.util.RestResult;
import cn.common.restresult.util.ResultEnum;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import cn.lyw.usermessage.mapper.mapper.UserMassageMapper;
import cn.lyw.usermessage.mapper.pojo.SmallMiUser;
import cn.lyw.usermessage.service.bo.AddUserMassageBO;
import cn.lyw.usermessage.service.bo.EditUserMassageBO;
import cn.lyw.usermessage.service.bo.GetLoginUserBO;
import cn.lyw.usermessage.service.domain.GetLoginUserDO;
import cn.lyw.usermessage.service.service.UserMassageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author   lyw
 * @Description 用户个人信息逻辑判断
 * @Date 15:35 2021/7/16
 * @Param
 * @return
 */
@Service
public class UserMassageServiceImpl implements UserMassageService {


    @Autowired
    private UserMassageMapper userMassageMapper;

    @Autowired
    private RedisServie redisServie;

    /**
     * @return
     * @Author lyw
     * @Description 根据当前登录用户查询个人信息
     * @Date 15:58 2021/7/16
     * @Param
     */
    @Override
    public List<GetLoginUserDO> getLoginUser(GetLoginUserBO bo) {

        //将参数copy到用户pojo类中
        SmallMiUser smallMiUser = BeanCopierUtil.copyObj(bo, SmallMiUser.class);
        //查询用户
        List<SmallMiUser> select = userMassageMapper.select(smallMiUser);
        //将返回值转成GetLoginUser
        List<GetLoginUserDO> getLoginUserDOS = BeanCopierUtil.copyList(select, GetLoginUserDO.class);
        return getLoginUserDOS;

    }


    /**
     * @return
     * @Author lyw
     * @Description 修改用户信息
     * @Date 17:05 2021/7/16
     * @Param
     */
    @Override
    public RestResult editUserMassage(EditUserMassageBO bo) {
        //将参数copy到用户pojo类中
        SmallMiUser smallMiUser = BeanCopierUtil.copyObj(bo, SmallMiUser.class);
        //根据用户id去查询用户的版本号
        SmallMiUser smallMiUser1 = userMassageMapper.selectByPrimaryKey(bo.getMiId());

        //修改用户信息中的修改时间，修改者和版本号
        smallMiUser.setModifyDate(new Date());
        smallMiUser.setModifieBy(bo.getMiId());
        //修改版本号
        String version = smallMiUser1.getVersion();
        int i1 = Integer.parseInt(version);
        Integer i2 = i1 + 1;
        String s = i2.toString();
        smallMiUser.setVersion(s);

        //修改用户信息
        int i = userMassageMapper.updateByPrimaryKeySelective(smallMiUser);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.EDIT_ERROR);
        }
        return new RestResult(ResultEnum.EDIT_SUCCESS);
    }


    /**
     * @return
     * @Author lyw
     * @Description 添加用户信息, 注册时使用
     * @Date 17:06 2021/7/16
     * @Param
     */
    @Override
    public RestResult addUserMassage(AddUserMassageBO bo) {
        //将参数copy到用户pojo类中
        SmallMiUser smallMiUser = BeanCopierUtil.copyObj(bo, SmallMiUser.class);

        //随机生成用户id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String next = objectIdGenerator.next();
        smallMiUser.setMiId(next);

        //将密码加密并存入
        EncryptUtil instance = EncryptUtil.getInstance();
        smallMiUser.setMiPassWord(instance.MD5(smallMiUser.getMiPassWord()));
        //添加创建时间，创建者和版本号
        smallMiUser.setCreationDate(new Date());
        smallMiUser.setCreatedBy(smallMiUser.getMiId());
        smallMiUser.setModifieBy(smallMiUser.getMiId());
        smallMiUser.setVersion("1");

        //添加用户信息
        int i = userMassageMapper.insertSelective(smallMiUser);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.ADD_ERROR);
        }
        return new RestResult(ResultEnum.ADD_SUCCESS);
    }


    /**
     * @return
     * @Author lyw
     * @Description 根据用户id去删除用户信息
     * @Date 17:25 2021/7/16
     * @Param
     */
    @Override
    public RestResult delUserMassage(String miId) {
        //根据用户id去删除用户信息
        int i = userMassageMapper.deleteByPrimaryKey(miId);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.DEL_ERROR);
        }
        return new RestResult(ResultEnum.DEL_SUCCESS);
    }

}
